package com.example.admintor.threehome.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.admintor.threehome.activity.MainActivity;
import com.example.admintor.threehome.data.Datas;
import com.example.admintor.threehome.R;
import com.example.admintor.threehome.data.ReciveBean;
import com.example.admintor.threehome.data.ReciveDateHander;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Admintor on 2017/12/5.
 */

public class OneFragment extends Fragment {
    @BindView(R.id.tem_num)
    TextView tem_num;

    @BindView(R.id.hum_num)
    TextView hum_num;

    @BindView(R.id.gz_num)
    TextView gz_num;

    @BindView(R.id.kr_num)
    TextView kr_num;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    tem_num.setText(reciveBean.getTem() + " ℃");
                    hum_num.setText(reciveBean.getHum() + " %");
                    gz_num.setText(reciveBean.getIll());
                    kr_num.setText(reciveBean.getSmoke());
                    break;
            }
        }
    };
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            handler.sendEmptyMessage(0x123);
            handler.postDelayed(this, 1000);
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_one, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    private ReciveDateHander reciveDateHander;
    private ReciveBean reciveBean;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        reciveDateHander = ((MainActivity) activity).getReciveDateHander();
        reciveBean = reciveDateHander.getReciveBean();
    }

    @Override
    public void onStart() {
        super.onStart();
        handler.postDelayed(runnable, 1000);
    }

    @Override
    public void onStop() {
        super.onStop();
        handler.removeCallbacks(runnable);
    }


}
